Skip to content

fix(hook): start generation history at the last compact boundary - #111

Open
lautaro-iridia wants to merge 1 commit into
langfuse:mainfrom
lautaro-iridia:fix/session-history-respects-compaction
Open

lautaro-iridia wants to merge 1 commit into
langfuse:mainfrom
lautaro-iridia:fix/session-history-respects-compaction

Conversation

@lautaro-iridia

Copy link
Copy Markdown

Problem

After a /compact (manual or automatic), Claude Code continues from the compact summary plus the preserved segment. Messages before the compact_boundary are no longer in the model's context window.

build_session_history rebuilds every generation input from the whole transcript, starting at byte 0. Two things go wrong:

  • Wrong input. Generations after a compaction show messages the model never saw. The summary, which the model did see, appears as just another user message in the middle of the old history.
  • Payload growth. The input keeps growing with the whole session instead of resetting at each compaction. Long sessions compact often, so this adds up.

Fix

Split the transcript at each compact_boundary row (type: system, subtype: compact_boundary). Each segment is one context window. A turn's prefix starts at its segment and contains:

  1. the compact summary (the isCompactSummary user row);
  2. the messages in compactMetadata.preservedSegment (headUuid to tailUuid), when present;
  3. the earlier turns of the same segment.

When work resumed right after an automatic compaction, the summary row opens the first turn of the segment. That case is handled, so the summary is not dropped.

SessionHistory gets a segment_start_index_by_user_row_uuid map, and prefix_for_turn slices from there. The default is 0, so transcripts without compactions produce exactly the same inputs as before.

Testing

  • New tests/integration/test_compacted_session_history.py, with 4 cases: a manual compact between turns, the preserved segment, work resumed after an auto compact, and two compactions in one session. All 4 fail on main and pass with this change.
  • Full suite: 282 pass. The single failure, test_state_lock_contention.py::test_entry_of_another_session_survives_the_emit, also fails on main (expired fixed timestamp, as noted in fix: ignore internal API request snapshots in turn assembly #107 and fix(hook): ignore typed non-chat rows that carry a chat role #108). Same result on Python 3.10.
  • ruff check --select F,E9 is clean, and the empty-payload smoke run exits 0.
  • Real transcript with 4 automatic compactions (30 turns): the per-turn prefixes match a reader-level implementation we have run in production since 2026-10-24, 30 of 30. The total size of the generation inputs drops from 13.5 MB to 3.4 MB.

Related: #110 / #108 (api-request-blob rows). That is independent of this change; for the real-transcript comparison those rows were filtered on both sides.

After a /compact (manual or automatic) the model continues from the compact
summary plus the preserved segment; earlier messages are no longer in its
context window. build_session_history rebuilt every generation input from the
whole transcript, so turns after a compaction showed messages the model never
saw and the input kept growing with the session.

Split the transcript at each compact_boundary row. A turn's prefix now starts at
its segment: the summary, the messages in compactMetadata.preservedSegment, then
the turns of that segment. Transcripts without compactions are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@CLAassistant

CLAassistant commented Oct 25, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants